package com.spider.admin.controller;


import com.spider.admin.exception.SpiderException;
import com.spider.admin.http.entity.PageResult;
import com.spider.admin.http.entity.User;
import com.spider.admin.service.account.AccountService;
import com.spider.admin.store.primary.model.AccountInfo;
import com.spider.admin.store.primary.model.AccountPermissions;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotBlank;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequiredArgsConstructor
@Validated
@RequestMapping(value = "/account", consumes = {"*/*"})
public class AccountController {
    @Resource
    private AccountService accountService;

    /**
     * 查询账户信息
     *
     * @param page
     * @param pageSize
     * @param accountName
     * @param realName
     * @param accountType
     * @param accountStatus
     * @return
     * @throws SpiderException
     */
    @GetMapping(value = "/lists", consumes = {"*/*"})
    public PageResult<AccountInfo> getAccounts(@RequestParam(required = false) Integer page, @RequestParam(required = false) Integer pageSize, @RequestParam(required = false) String accountName, @RequestParam(required = false) String realName, @RequestParam(required = false) Integer accountType, @RequestParam(required = false) String accountStatus) throws SpiderException {

        return accountService.queryAccounts(page, pageSize, accountName, realName, accountType, accountStatus);
    }

    /**
     * 查询账户信息
     *
     * @param accountName
     * @return
     * @throws SpiderException
     */
    @GetMapping(value = "/query/{accountName}")
    public AccountInfo getAccountByName(@PathVariable("accountName") String accountName) throws SpiderException {
//        log.info("查询账户:{}", accountName);
        return accountService.queryAccountByName(accountName);
    }

    /**
     * 添加账户
     *
     * @param accountInfo
     * @return
     */
    @PostMapping(value = "/add")
    public int addAccount(@Validated(AccountInfo.Save.class) @RequestBody AccountInfo accountInfo) throws SpiderException {
        return accountService.saveAccount(accountInfo);
    }

    /**
     * 更新账户
     *
     * @param accountInfo
     * @param accountName
     * @return
     */
    @PutMapping(value = "/update/{accountName}")
    public int updateAccount(@Validated(AccountInfo.Update.class) @RequestBody AccountInfo accountInfo, @PathVariable("accountName") @NotBlank String accountName) throws SpiderException {
//        log.info("更新账户:{}", accountName);
        return accountService.updateAccount(accountInfo, accountName);
    }

    /**
     * 更新账户
     *
     * @param accountInfo
     * @param accountName
     * @return
     */
    @PutMapping(value = "/update-owner/{accountName}")
    public int updateAccountByOwner(@Validated(AccountInfo.Update.class) @RequestBody AccountInfo accountInfo, @PathVariable("accountName") @NotBlank String accountName) throws SpiderException {
//        log.info("更新账户:{}", accountName);
        return accountService.updateAccountByOwner(accountInfo, accountName);
    }

    /**
     * 激活账户
     *
     * @param accountName
     * @return
     */
    @PutMapping(value = "/active/{accountName}")
    public int activeAccount(@PathVariable("accountName") @NotBlank String accountName) throws SpiderException {
//        log.info("激活账户:{}", accountName);
        return accountService.activeAccount(accountName);
    }

    /**
     * 归档账户
     *
     * @param accountName
     * @return
     */
    @PutMapping(value = "/archive/{accountName}")
    public int archiveAccount(@PathVariable("accountName") @NotBlank String accountName) throws SpiderException {
//        log.info("归档账户:{}", accountName);
        return accountService.archiveAccount(accountName);
    }

    @GetMapping("/perms")
    public List<AccountPermissions> getAllPerms() throws SpiderException {
        return accountService.getAllPerms();
    }

    @PutMapping(value = "/resetPwd/{accountName}")
    public String resetAccountPwd(@PathVariable("accountName") @NotBlank String accountName) throws SpiderException {
        return accountService.updateAccountPwd(accountName, null);
    }


    @PostMapping("/login")
    public AccountInfo login(@RequestBody User user) throws SpiderException {
        return accountService.login(user.getUsername(), user.getPassword());
    }
}

